/*
 * Copyright (C) 2012 Le Tuan Anh <anh.takaji@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ezcms.client;

import ezcms.ejb.ConferenceServiceRemote;
import ezcms.ejb.UserAccountServiceRemote;
import ezcms.exceptions.PresentationException;
import java.util.Properties;
import javax.naming.InitialContext;

/**
 * For locating EJBs
 *
 * @author Le Tuan Anh <anh.takaji@gmail.com>
 */
public class EJBLocator {

    private InitialContext context = null;
    private static EJBLocator locator = null;

    static {
        locator = new EJBLocator();
    }

    private EJBLocator() {
        try {
            Properties props = new Properties();
            props.setProperty("java.naming.factory.initial",
                    "com.sun.enterprise.naming.SerialInitContextFactory");
            props.setProperty("java.naming.factory.url.pkgs",
                    "com.sun.enterprise.naming");
            props.setProperty("java.naming.factory.state",
                    "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
            props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
            props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
            context = new InitialContext(props);
//            context = new InitialContext();
        } catch (Exception ex) {
            //TODO: Log ex
            ex.printStackTrace();
        }
    }

    public static EJBLocator getInstance() {
        return locator;
    }

    public UserAccountServiceRemote getUserServiceFacade() throws PresentationException {
        try {
            Object obj = context.lookup("ezcms.UserAccountService");
            return (UserAccountServiceRemote) obj;
        } catch (Exception ex) {
            //TODO: Log
            //ex.printStackTrace();
            throw new PresentationException(ex);
        }
    }

    public ConferenceServiceRemote getConferenceServiceFacade() throws PresentationException {
        try {
            Object obj = context.lookup("ezcms.ConferenceService");
            return (ConferenceServiceRemote) obj;
        } catch (Exception ex) {
            //TODO: Log
            //ex.printStackTrace();
            throw new PresentationException(ex);
        }
    }
}
